//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Files.Components;
using CA = ComponentArt.Web.UI;

namespace CommunityServer.ControlPanel.Files
{
	public class UserDownloadList : BaseFilesGridControl
	{
		protected CA.Grid Grid1;
		protected CommunityServer.ControlPanel.Tools.Reports.ActivityQueryControl ActivityQuery;
		CSContext csContext;

		private void Page_Load(object sender, EventArgs e)
		{
			if (Request.QueryString["csv"] == "1")
				Response.Redirect("~/ControlPanel/Files/UserDownloadCsv.aspx?" + Page.Request.QueryString.ToString(), true);

			csContext = CSContext.Current;
			this.CurrentGridRowSize = 1;
			ActivityQuery.DestinationURL = "~/ControlPanel/Files/UserDownloads.aspx?pid=" + csContext.GetIntFromQueryString("pid",-1).ToString();

			if (!this.IsPostBack && !Grid.IsCallback)
			{
				buildGrid();
				Grid.DataBind();
			}
		}

		protected override void buildGrid()
		{
			DateTime BegReportDate = DateTime.Parse("1/1/1900");
			DateTime EndReportDate = DateTime.Parse("1/1/1900");

			if (csContext.QueryString["brd"] == null || csContext.QueryString["erd"] == null || csContext.QueryString["pid"] == null)
			{
				Grid.Visible = false;
				return;
			}

			if (Globals.IsDate(csContext.QueryString["brd"]))
			{
				BegReportDate = DateTime.Parse(csContext.QueryString["brd"]);
				ActivityQuery.BegSearchReportDatePicker = BegReportDate;
				ActivityQuery.BegSearchReportDateCalendar = BegReportDate;
			}

			if (Globals.IsDate(csContext.QueryString["erd"]))
			{
				EndReportDate = DateTime.Parse(csContext.QueryString["erd"]);
				ActivityQuery.EndSearchReportDatePicker = EndReportDate;
				ActivityQuery.BegSearchReportDateCalendar = EndReportDate;
			}

			int postID = csContext.GetIntFromQueryString("pid",-1);
		    UserDownloadSet uds;

			this.GridMode = CA.GridRunningMode.Callback;			
			if(this.GridMode == CA.GridRunningMode.Callback)
			{
				uds = CommunityServer.Files.UserDownloads.GetUserDownloads(postID, BegReportDate, EndReportDate, Grid.CurrentPageIndex, this.DefaultPageSize);
				Grid.ManualPaging = true;
			}
			else
				uds = CommunityServer.Files.UserDownloads.GetUserDownloads(postID, BegReportDate, EndReportDate, 0, int.MaxValue - 1); 

			Grid.DataSource = uds.UserDownloads;
			this.RecordCount = uds.TotalRecords;

			base.buildGrid();
		}

		protected override void ConfigureGrid()
		{
			AddGridPagerTemplate("DownloadDate");
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new EventHandler(this.Page_Load);
		}

		#endregion
	}
}